package cc.mrbird.febs.business.controller;

import cc.mrbird.febs.business.domain.Community;
import cc.mrbird.febs.business.service.CommunityService;
import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.domain.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("community")
@Api(description = "小区信息接口")
public class CommunityController extends BaseController {

    @Autowired
    private CommunityService communityService;

    private String message;

    @GetMapping
    @RequiresPermissions("community:view")
    @ApiOperation(value = "小区列表" ,  notes="小区列表")
    public Map<String, Object> communityList(QueryRequest queryRequest, Community community) {
        return getDataTable(communityService.findCommunitys(community, queryRequest));
    }

    @GetMapping("check/{communityName}")
    @ApiOperation(value = "小区列表" ,  notes="小区列表")
    public boolean checkCommunityName(@NotBlank(message = "{required}") @PathVariable String communityName) {
        Community result = this.communityService.findByName(communityName);
        return result == null;
    }


    @Log("新增小区")
    @PostMapping
    @RequiresPermissions("community:add")
    public void addCommunity(@Valid @ApiParam("小区对象") @RequestBody Community community) throws FebsException {
        try {
            this.communityService.createCommunity(community);
        } catch (Exception e) {
            message = "新增小区失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除小区")
    @DeleteMapping("/{communityIds}")
    @RequiresPermissions("community:delete")
    public void deleteCommunitys(@NotBlank(message = "{required}") @ApiParam(value = "小区id", required = true) @PathVariable String communityIds) throws FebsException {
        try {
            String[] ids = communityIds.split(StringPool.COMMA);
            this.communityService.deleteCommunitys(ids);
        } catch (Exception e) {
            message = "删除小区失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改小区")
    @PutMapping
    @RequiresPermissions("community:update")
    public void updateCommunity(@Valid @ApiParam("需更新的小区对象") @RequestBody Community community) throws FebsException {
        try {
            this.communityService.updateCommunity(community);
        } catch (Exception e) {
            message = "修改小区失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @PostMapping("excel")
    @RequiresPermissions("community:export")
    public void export(QueryRequest queryRequest, Community community, HttpServletResponse response) throws FebsException {
        try {
            List<Community> communitys = this.communityService.findCommunitys(community, queryRequest).getRecords();
            ExcelKit.$Export(Community.class, response).downXlsx(communitys, false);
        } catch (Exception e) {
            message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
